package org.rain.common.util.json.status;

import org.rain.common.util.json.AbsJsonState;
import org.rain.common.util.json.ContextType;
import org.rain.common.util.json.JsonFsm;

/**
 * created by yangtong on 2025/4/14 22:41:56
 * <br/>
 * 表示正在读取数组类型的value
 */
public class InArrayValueState extends AbsJsonState {
    @Override
    public void handleNonWhitespace(JsonFsm fsm, char c) {
        if (c == '"') {
            //字符串
            fsm.cleanBuffer();
            fsm.changeState(IN_STRING_VALUE);
        } else if (Character.isDigit(c) || c == '-') {
            //数字
            fsm.changeState(IN_NUMBER_VALUE);
            //先清空缓存，再写入数字
            fsm.cleanBuffer();
            fsm.writeBuffer();
        } else if (c == 't') {
            //true类型
            fsm.changeState(IN_LITERAL_VALUE);
            fsm.cleanBuffer();
            fsm.writeBuffer();
            fsm.targetLiteral("true");
        } else if (c == 'f') {
            //false类型
            fsm.changeState(IN_LITERAL_VALUE);
            fsm.cleanBuffer();
            fsm.writeBuffer();
            fsm.targetLiteral("false");
        } else if (c == 'n') {
            //null类型
            fsm.changeState(IN_LITERAL_VALUE);
            fsm.cleanBuffer();
            fsm.writeBuffer();
            fsm.targetLiteral("null");
        } else if (c == '{') {
            //表示嵌套对象
            fsm.nesting(ContextType.OBJECT);
            fsm.changeState(EXPECT_KEY);
        } else if (c == '[') {
            //表示嵌套数组
            fsm.nesting(ContextType.ARRAY);
            fsm.changeState(IN_ARRAY_VALUE);
        } else if (c == ']') {
            //弹栈
            if (fsm.deNesting() == 0) {
                fsm.changeState(END);
            }
        }
        else {
            throw new RuntimeException("意料之外的字符，【" + c + "】");
        }
    }
}
